<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                <el-input
                        v-model="form.title"
                        autocomplete="off"
                        placeholder="请输入标题"
                ></el-input>
            </el-form-item>
			<el-form-item label="权重" :label-width="formLabelWidth" prop="weight">
				<el-input-number v-model="form.weight"
                        autocomplete="off"
                        placeholder="请输入标题">
				</el-input-number>
            </el-form-item>
            <el-form-item
                label="列表模板"
                :label-width="formLabelWidth"
                prop="listtp"
            >
                <fortress-select
                v-model="form.listtp"
                :dictList="listtpList"
                placeholder="请选择列表模板"
                clearable
                ></fortress-select>
            </el-form-item>            
        </el-form>
        <div slot="footer" class="dialog-footer" >
            <el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
            <el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
    import { cmscusttagInfo, cmscusttagSave, cmscusttagEdit } from "@/api/cms/cmscusttag.js";
    import { cmstemplateFilter } from "@/api/cms/cmstemplate.js";
    
    export default {
        data() {
            return {
                visible: false,
                form: {
                    title:"",
                    listtp:"",
					weight:1,
                },
                formLabelWidth: "120px",
                rules: {
                    title:[
                          { required: true, message: "请输入标题", trigger: "blur" },
                      ],
					weight:[
						{ required: true, message: "请输入权重", trigger: "blur" },
					],
                },
                listtpList:[]               
            };
        },
        created() {
            this.qureyTp();
        },
        methods: {
            //获取模板列表
            qureyTp(){
            this.listtpList = [];
            cmstemplateFilter("tag").then((resp) => {
                if (resp.code == 0) {
                if (resp.data) {
                    resp.data.forEach((item) => {
                    this.listtpList.push({ code: item.fileName, name: item.fileName });
                    });
                }
                }
            });
            },
            init(id) {
                this.resetForm();
                //修改
                if (id) {
                    cmscusttagInfo(id).then((resp) => {
                        if (resp.code == 0) {
                            this.$nextTick(() => {
                                this.form = {
                                    ...this.form,
                                    ...resp.data,
                                };
                            });
                        } else {
                            this.$msg({
                                message: resp.msg,
                                type: "error",
                            });
                        }
                    });
                }
                this.visible = true;
            },
            //提交表单数据
            submitForm() {
                this.$refs["form"].validate((valid) => {
                if (valid) {
                    //修改
                    if (this.form.id) {
                        cmscusttagEdit(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                            this.$emit("refreshData");
                            this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    //新增
                    } else {
                        cmscusttagSave(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                                this.$emit("refreshData");
                                this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    }
                } else {
                    console.log("error submit!!");
                    return false;
                }
                });
            },
            resetForm() {
                this.form = {
                    title:"",
                    listtp:"",
					weight:1,
                };
                this.$nextTick(() => {
                    if (this.$refs["form"]) {
                        this.$refs["form"].resetFields();
                    }
                });
            },
        },
    };
</script>
<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
        height: auto;
        max-height: 274px;
        padding: 7px;
        overflow: hidden;
        overflow-y: auto;
        font-weight: normal;
    }
</style>
